package com.koron.css2.newProjectCenter.function.mapper;

import com.koron.css2.newProjectCenter.function.base.DictAction;
import com.koron.css2.newProjectCenter.function.bean.Dictionary;
import com.koron.css2.newProjectCenter.function.bean.DictionaryDetail;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface DictionaryMapper {
    List<Dictionary> findDictionaryListByPage(DictAction dictAction);

    @Select("select count(*) > 0 from pub_data_dictionary where ID = #{id} and ISDELETED = false")
    boolean dictIdIsExist(@Param("id") String id);

    @Select("select * from pub_data_dictionary_detail where DICTID = #{id} and ISDELETED = false")
    List<DictionaryDetail> getDetailListByDictId(@Param("id") String id);

    @Update("update pub_data_dictionary_detail set ISDELETED = true where DICTID = #{dictId}")
    void deleteDictDetailByDictId(@Param("dictId") String dictId);

    @Update("update pub_data_dictionary set ISDELETED = true where ID = #{dictId} ")
    void deleteDictByDictId(@Param("dictId") String dictId);

    @Update("update pub_data_dictionary_detail set ISDELETED = true where DICTID = #{dictId} and ID = #{detailId}")
    void deleteDictDetailByDictIdAndDetailId(@Param("dictId") String dictId, @Param("detailId") String detailId);

    void updateDictByDictId(@Param("dictId") String dictId, @Param("dictName") String dictName, @Param("remark") String remark);

    void updateDictDetailByDictIdAndDetailId(
            @Param("dictId") String dictId,
            @Param("detailId") String detailId,
            @Param("detailName") String detailName,
            @Param("detailValue") Integer detailValue
    );

    @Select("select count(*) > 0 from pub_data_dictionary where CODE = #{dictCode} and ISDELETED = false")
    boolean dictCodeIsExist(@Param("dictCode") String dictCode);

    @Insert("insert into pub_data_dictionary(ID, NAME, CODE, REMARK, CREATETIME, CREATEBY) value (#{dictId}, #{dictName}, upper(#{dictCode}), #{remark}, now(), #{createName})")
    void insertDict(DictAction dictAction);

    @Insert("insert into pub_data_dictionary_detail(ID, DICTID, NAME, VALUE, CREATEBY, CREATETIME)" +
            "value (#{detailId}, #{dictId}, #{detailName}, #{detailValue}, #{createName}, now())")
    void insertDictDetail(DictAction dictAction);

    @Select("select count(*) > 0 from pub_data_dictionary_detail where DICTID = #{dictId} and VALUE = #{detailValue} and ISDELETED = false")
    boolean dictDetailValueIsExist(@Param("dictId") String dictId, @Param("detailValue") Integer detailValue);
}
